#Load Conjoint Package#
library(cjoint)
library(psych)

#Set Working Directory#
setwd("~/Documents/Dissertation")

#Load Conjoint Data#
conjoint_data <- read.qualtrics("Conjoint Experiment Data.csv", responses=c("QID4", "QID9", "QID13", "QID17", "QID21", "QID48", "QID52", "QID56", "QID60", "QID64"), covariates=c("QID67", "QID68", "QID69", "QID37", "QID40"), respondentID=NULL)
View(conjoint_data)

#Create Indicator Variables for Democrat & Republican Respondents#
conjoint_data$democrat=as.numeric(conjoint_data$QID37=="1")
conjoint_data$demleaner=as.numeric(conjoint_data$QID40=="2")
conjoint_data$dems=as.numeric(conjoint_data$democrat + conjoint_data$demleaner)

conjoint_data$republican=as.numeric(conjoint_data$QID37=="2")
conjoint_data$repleaner=as.numeric(conjoint_data$QID40=="1")
conjoint_data$reps=as.numeric(conjoint_data$republican + conjoint_data$repleaner)

#Reset Baseline Candidate Attributes#
baselines <- list()
baselines$"Occupation" <- "Lawyer"
baselines$"Political.Party" <- "Republican"
baselines$"Political.Ideology" <-"Conservative"
baselines$"Race" <- "White"
baselines$"Religion" <- "Protestant"
baselines$"Sex" <- "Male"

results <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data, selected!="NA"), cluster=TRUE, respondent.id="respondentIndex", baselines=baselines)
summary(results)
plot(results, xlab="Change in Pr(Prefer Candidate for Congress)", label.baseline=TRUE)

#Drop Observations w/ Missing Data#
conjoint_data2 <- na.omit(conjoint_data)

#Convert Character Variables to Numeric#
QID67.n <- as.numeric(conjoint_data2$QID67)
QID68.n <- as.numeric(conjoint_data2$QID68)
QID69.n <- as.numeric(conjoint_data2$QID69)

#Bind Variables for Principal Component Analysis#
Religiosity.M <- cbind(QID67.n, QID68.n, QID69.n)

#Run PCA#
cor(Religiosity.M)
pca <- princomp(Religiosity.M, scores = T, cor = T)
summary(pca)
loadings(pca)
screeplot(pca, type = "line", main = "Religiosity Factors Scree Plot")
pca$scores[1:100,]
summary(pca$scores)

#Run Factor Analysis with Varimax Rotation#
factor1 <- factanal(Religiosity.M, rotation = "varimax", factors = 1, scores = "regression")
summary(factor1)
loadings(factor1)
factor1$scores[1:100,]
scores <- factor1$scores
conjoint_data.factor <- cbind(conjoint_data2, scores)
summary(scores)

#Create Dummy Variables for Levels of Religiosity
conjoint_data.factor$lreligiosity <- as.numeric(conjoint_data.factor$Factor1 > "0.2718634")
conjoint_data.factor$hreligiosity <- as.numeric(conjoint_data.factor$Factor1 < "0.2718634")

#Analysis#
resultslreligiosity <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, lreligiosity==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultslreligiosity)
plot(resultslreligiosity, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)

resultshreligiosity <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, hreligiosity==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultshreligiosity)
plot(resultshreligiosity, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)

#Analyze Data by Party#
resultsRep <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data2, reps==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsRep)
plot(resultsRep, xlab="Change in Pr(Prefer Candidate for Congress)", label.baseline=TRUE)
plot(resultsRep, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)
plot(resultsRep, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE, ci = .90)

resultsDem <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data2, dems==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsDem)
plot(resultsDem, xlab="Change in Pr(Prefer Candidate for Congress)", label.baseline=TRUE)
plot(resultsDem, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)
plot(resultsDem, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE, ci = .90)

#Create Indicator Variables for Party by Religiosity#
conjoint_data.factor$ldems=as.numeric(conjoint_data.factor$dems + conjoint_data.factor$lreligiosity == "2")
conjoint_data.factor$hdems=as.numeric(conjoint_data.factor$dems + conjoint_data.factor$hreligiosity == "2")
conjoint_data.factor$lreps=as.numeric(conjoint_data.factor$reps + conjoint_data.factor$lreligiosity == "2")
conjoint_data.factor$hreps=as.numeric(conjoint_data.factor$reps + conjoint_data.factor$hreligiosity == "2")

#Analyses by Party & Religiosity#
resultsdlr <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, ldems==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsdlr)
plot(resultsdlr, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)

resultsdhr <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, hdems==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsdhr)
plot(resultsdhr, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)

resultsrlr <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, lreps==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsrlr)
plot(resultsrlr, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)

resultsrhr <- amce(selected ~ `Political.Party` + `Political.Ideology` + `Occupation` +`Race` + `Religion` + `Sex`, data=subset(conjoint_data.factor, hreps==1, selected!="NA"), cluster=TRUE, respondent.id="respondent", baselines=baselines)
summary(resultsrhr)
plot(resultsrhr, xlab="Change in Pr(Prefer Candidate for Congress)", colors="black", text.color="black", label.baseline=TRUE)
